package edu.gatech.cs4220;

import android.media.RingtoneManager;
import android.media.AudioManager;
import java.util.Map;

/**
 * A class containing the Android settings that a LocationProfile will apply
 * @author C. Sean Young
 *
 */
//Package private class, so we can treat it as a struct
class ProfileData implements java.io.Serializable
{
	private static final long serialVersionUID = 1301747022593248163L;
	/**
	 * The path to the ringtone this profile sets, or {@code null} if this profile does not specify
	 */
	android.net.Uri ringtone;
	/**
	 * Integer.MIN_VALUE if not set in this profile
	 * @see android.media.AudioManager.RINGER_MODE_NORMAL
	 * @see android.media.AudioManager.RINGER_MODE_SILENT
	 * @see android.media.AudioManager.RINGER_MODE_VIBRATE
	 */
	//Android really needs to start using enums. These int constant pools are too hard to keep track of
	int ringerMode = Integer.MIN_VALUE;
	
	/**
	 * Maps streams to their respective volumes.
	 * Because of how many streams there are, this is preferable to
	 * just having a ton of private variables
	 */
	//I would use android.util.SpareIntArray, but sadly it does not support iteration over key/pair values
	Map<Integer, Integer> streamVolumes = new java.util.HashMap<Integer, Integer>(2);
	
	private transient int ringVolume = Integer.MIN_VALUE; //Cached for performance, stored in streamVolumes 
	private transient int notificationVolume = Integer.MIN_VALUE; //Cached for performance, stored in streamVolumes
	private transient int systemVolume = Integer.MIN_VALUE; //Cached for performance, stored in streamVolumes
	private transient android.content.Context APP_CONTEXT;
	
	ProfileData(final android.content.Context APP_CONTEXT)
	{
		this.APP_CONTEXT = APP_CONTEXT;
	}
	
	public void apply()
	{
		if(ringtone != null)
			RingtoneManager.setActualDefaultRingtoneUri(APP_CONTEXT, RingtoneManager.TYPE_RINGTONE, ringtone);
		AudioManager audioMan = (AudioManager)APP_CONTEXT.getSystemService(android.content.Context.AUDIO_SERVICE);
		if(ringerMode > Integer.MIN_VALUE)
		{
			audioMan.setRingerMode(ringerMode);
		}
		for(Map.Entry<Integer, Integer> streamVolumePair : streamVolumes.entrySet())
		{
			audioMan.setStreamVolume(streamVolumePair.getKey(), streamVolumePair.getValue(), 0);
		}
	}
	
	private void updateCache()
	{
		Integer temp;
		temp = streamVolumes.get(AudioManager.STREAM_RING);
		ringVolume = temp == null ? Integer.MIN_VALUE : temp;
		temp = streamVolumes.get(AudioManager.STREAM_NOTIFICATION);
		notificationVolume = temp == null ? Integer.MIN_VALUE : temp;
		temp = streamVolumes.get(AudioManager.STREAM_SYSTEM);
		systemVolume = temp == null ? Integer.MIN_VALUE : temp;
	}
	
	private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException
	{
		in.defaultReadObject();
		APP_CONTEXT = geoprofiles.APP_CONTEXT;
	}


	 
}